package br.com.comanda;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import br.com.comanda.customadapters.AccountAdapter;
import br.com.comanda.service.AccountService;
import br.com.comanda.vos.Account;

public class ComandaActivity extends Activity {

	Button btnNewAccount;
	Button btnAddItem;
	String selectedTable = null;

	List<Map<String, String>> accounts = new ArrayList<Map<String, String>>();
	List<Account> accountList = new ArrayList<Account>();
	AccountService accountService = new AccountService();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	public void init() {
		setContentView(br.com.comanda.R.layout.main);

		this.accountList = this.accountService.readAllAccounts();

		this.btnNewAccount = (Button) findViewById(br.com.comanda.R.id.btnNovaConta);

		this.btnNewAccount.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent i = new Intent(ComandaActivity.this, NovaContaActivity.class);
				startActivityForResult(i, 1);
			}
		});

		ArrayAdapter<Account> ad = new AccountAdapter(this, R.layout.account_list, this.accountList);
		ListView lv = (ListView) findViewById(R.id.lstContas);
		lv.setAdapter(ad);
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		registerForContextMenu(lv);

	}

	// Retorno da NovaContaActivity
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == 1) {
			if (data.getExtras() != null) {
				ComandaActivity.this.selectedTable = data.getExtras().getString("Valor");
				Log.i("SELECTED", this.selectedTable.toString());
				Map<String, String> account = new Hashtable<String, String>();
				account.put("cliente", data.getExtras().getString("cliente"));
				account.put("mesa", ComandaActivity.this.selectedTable);

				ComandaActivity.this.accounts.add(account);
			}
		}
		init();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.lstContas) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle(this.accountList.get(info.position).getClient());

			String[] menuItems = getResources().getStringArray(R.array.accountMenu);
			for (int i = 0; i < menuItems.length; i++) {
				menu.add(0, i, 0, menuItems[i]);
			}

		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		String[] menuItems = getResources().getStringArray(R.array.accountMenu);
		Account account = this.accountList.get(info.position);
		if (menuItems[menuItemIndex].equals("Adicionar Item")) {
			Intent i = new Intent(ComandaActivity.this, AddItemActivity.class);
			i.putExtra("accountId", account.getId());
			startActivityForResult(i, 2);
		}

		if (menuItems[menuItemIndex].equals("Fechar Conta")) {
			this.accountService.closeAccount(account.getId());
			init();
		}

		if (menuItems[menuItemIndex].equals("Editar Conta")) {
			Intent i = new Intent(ComandaActivity.this, EditItemActivity.class);
			i.putExtra("accountId", account.getId());
			startActivityForResult(i, 3);
		}

		return true;
	}
}